﻿#ifndef GLOBALDATA_H
#define GLOBALDATA_H

#include <QString>
#include <QList>
#include <QPixmap>
#include <Map>

class QXmlStreamReader;
// ---------------------- Pin 结构体 ----------------------
struct Pin {
    Pin() {}
    int index = -1;
    bool isInputPin = false;
    QString unitId;
    QString name;
    QString unit;
    QString desc;
    QString dir;
    QString value;
    bool operator==(const Pin& lhs)
    {
        return lhs.unitId == unitId &&
               lhs.index == index &&
               lhs.isInputPin == isInputPin &&
               lhs.name == name &&
               lhs.unit == unit &&
               lhs.desc == desc &&
               lhs.dir == dir &&
               lhs.value == value;
    }
};

// ---------------------- Unit 类 ----------------------
class Unit
{
public:
    QString id;
    QString type;
    QString name;
    QString desc;
    QString iconPath;

    // 位置信息
    struct Pos
    {
        QString x;
        QString y;
        QString w;
        QString h;
        QString angle;
    } pos;

    //参数信息
    struct Constaut
    {
        int index = -1;
        QString name;
        QString value;
        QString unit;
        QString desc;
        bool operator==(const Unit::Constaut& lhs)
        {
            return lhs.index == index &&
                   lhs.name == name &&
                   lhs.value == value &&
                   lhs.unit == unit &&
                   lhs.desc == desc;
        }
    };

    QList<Pin> inputPins;    // 输入引脚列表
    QList<Pin> outputPins;   // 输出引脚列表
    QList<Constaut> constants; //参数列表

    bool operator==(const Unit& lhs);
    void setInputPins(const QList<Pin> &pins ) {inputPins = pins;}
    void setOutputPins(const QList<Pin> &pins ) {outputPins = pins;}

    QString codeType;        // code/dll/sin
    QString codeValue;
    Unit() {}
};

class Units
{
public:
    QList<Unit> units;
    QString name;
    Units(){}
};

// ---------------------- 单例 GlobalData 类 ----------------------
class GlobalData {
public:
    static GlobalData& getInstance()
    {
        static GlobalData instance;
        return instance;
    }

    QList<Unit>& getUnits() {return m_units;}
    QList<Units>& getCatalog() {return m_catalog;}
    QList<std::pair<Pin, Pin>>& getPinMap() {return m_pinMap;}

    const Unit *findUnitById(QString id) const;
    bool initCatalog(const QString &fileName);
    bool initGlobalUnits(const QString &fileName);

    void setCurPixmap(const QPixmap& pm) {m_curPixmap = pm;}
    QPixmap& getCurPixmap() {return m_curPixmap;}
    void setCurUnit(const Unit& unit) {m_curUnit = unit;}
    Unit& getCurUnit() {return m_curUnit;}
private:
    GlobalData() {}
    QList<Unit> m_units;
    QList<Units> m_catalog;
    QPixmap m_curPixmap;
    Unit m_curUnit;
    QList<std::pair<Pin, Pin>> m_pinMap;

    GlobalData(const GlobalData&) = delete;
    GlobalData& operator=(const GlobalData&) = delete;
};


#endif // GLOBALDATA_H
